//
//  XVVSelectedExhibitionHall.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/8.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVVSelectedExhibitionHall.h"
#import "XXTool.h"
#import "ProjectWithArrowButton.h"

@interface XVVSelectedExhibitionHall ()

@property (nonatomic,strong) UIImageView *backgroundImg;

@property (nonatomic, strong) UIScrollView *buttonContent;

@end

@implementation XVVSelectedExhibitionHall
#pragma mark getter setter
-(UIImageView*)backgroundImg
{
    if (_backgroundImg==nil)
    {
        _backgroundImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"商贸配对背景图"]];
        _backgroundImg.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_backgroundImg];
    }
    return _backgroundImg;
}

-(UIScrollView *)buttonContent
{
    if (_buttonContent == nil)
    {
        _buttonContent = [UIScrollView new];
        [self addSubview:_buttonContent];
    }
    return _buttonContent;
}

-(void)setHallButtonArray:(NSMutableArray<XMLHallNumberListModel *> *)hallButtonArray
{
    _hallButtonArray = [[NSMutableArray alloc] init];

    for (int i=0; i<hallButtonArray.count+1; i++)
    {
        
        ProjectWithArrowButton *temp = [[ProjectWithArrowButton alloc] init];
        [temp addTarget:self Select:@selector(clickHallSelectButton:) ForEvent:DKButtonEventPressedEnd];
        if (i==0)
        {
            [temp setText:NSLocalizedString(@"所有参展商", nil)];
        }
        else
        {
            XMLHallNumberListModel *hallTemp = hallButtonArray[i-1];
            [temp setText:hallTemp.el_title];
        }
        [self.buttonContent addSubview:temp];
        [_hallButtonArray addObject:temp];
    }
    [self refreshLayout:self.frame];
}

-(void)setViewData:(NSMutableArray<XMLMapType *> *)array
{
    if (_hallButtonArray==nil)
    {
        _hallButtonArray = [[NSMutableArray alloc] init];
    }
    else
    {
        for (UIView *temp in _hallButtonArray)
        {
            [temp removeFromSuperview];
        }
        [_hallButtonArray removeAllObjects];
    }
    for (int i=0; i<array.count; i++)
    {
        ProjectWithArrowButton *temp = [[ProjectWithArrowButton alloc] init];
        [temp addTarget:self Select:@selector(clickHallMapSelectButton:) ForEvent:DKButtonEventPressedEnd];
        XMLMapType *hallTemp = array[i];
        [temp setText:hallTemp.maplistname];
        temp.tag = [hallTemp.mtype intValue];
        [self addSubview:temp];
        [_hallButtonArray addObject:temp];
    }
    _isShow = YES;
    self.hidden = NO;
    [self refreshLayout:self.frame];
}

-(void)initParameter
{
    _isShow = NO;
}

-(void)initUI
{
    self.hidden = YES;
    self.clipsToBounds = YES;
    
    [self backgroundImg];
    _backgroundImg.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width
                                      , [UIScreen mainScreen].bounds.size.height-64);
}

-(void)refreshLayout:(CGRect)frame
{
    self.buttonContent.frame = self.backgroundImg.frame;
    for (int i=0; i<_hallButtonArray.count; i++)
    {
        ProjectWithArrowButton *temp = _hallButtonArray[i];
        temp.frame = CGRectMake((frame.size.width-245)/2
                                , 60+(50+20)*i
                                , 245
                                , 50);
    }
    self.buttonContent.contentSize = CGSizeMake(self.buttonContent.xx_width
                                                , 60*2 + 50 * _hallButtonArray.count + 20 * (_hallButtonArray.count - 1));
}

-(void)clickHallSelectButton:(DKButton*)hallButton
{
    if ([_delegate respondsToSelector:@selector(selectedExhibitionhallDidClickHallWithHallText:)])
    {
        [_delegate selectedExhibitionhallDidClickHallWithHallText:hallButton.text];
    }
    [self dismiss];
}

-(void)clickHallMapSelectButton:(DKButton*)hallButton
{
    if ([_delegate respondsToSelector:@selector(selectedExhibitionhallDidClickHallWithHallMapType:)])
    {
        [_delegate selectedExhibitionhallDidClickHallWithHallMapType:hallButton.tag];
    }
}

#pragma mark api
-(void)showStausChange
{
    if (!_isShow)
    {
        [self show];
    }
    else
    {
        [self dismiss];
    }
}

-(void)show
{
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);
    self.hidden = NO;
    [[XXTool getViewController:self].view bringSubviewToFront:self];
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width
                                                 , [UIScreen mainScreen].bounds.size.height-64);
                     }];
    _isShow = !_isShow;
}

-(void)dismiss
{
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);
                     } completion:^(BOOL finished) {
                         if (finished)
                         {
                             self.hidden = YES;
                         }
                     }];
    _isShow = !_isShow;
}

@end
